package com.example.pet.service;

import com.example.pet.dao.ShopDao;
import com.hj.pet.api.ShopService;
import com.hj.pet.constant.Code;
import com.hj.pet.constant.Constants;
import com.hj.pet.model.ReturnT;
import com.hj.pet.model.Shop;
import com.hj.pet.model.UserInfo;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImp implements ShopService {
    @Autowired
    ShopDao shopDao;
    @Override
    public int save(Shop shop){
        System.out.println(shop);
        return shopDao.save(shop);
    }

    @Override
    public int updateByShopId(Shop shop){
        System.out.println(shop);
        return shopDao.updateByShopId(shop);
    }

    @Override
    public Shop findShopByShopId(String shop_id){
        return shopDao.findShopByShopId(shop_id);
    }

    @Override
    public List<Shop> findShopByName(String shop_name){
        return shopDao.findShopByName(shop_name);
    }

    @Override
    public List<Shop> findShopByMaster(String master_id){
        return shopDao.findShopByMaster(master_id);
    }

    @Override
    public List<Shop> findShop(Shop shop){
        return shopDao.findShop(shop);
    }

    @Override
    public int deleteById(String shop_id){
        return shopDao.deleteById(shop_id);
    }

    @Override
    public ReturnT<?> addShop(Shop shop){
        // 检查master-id是否存在 用户是否存在
        // 检查shop-id是否被占用
        // 同一个人不能开同名店
        // vip用户无开店限制，普通用户最多开5个店
        // 开店之后用户自动晋升为店主seller
        UserInfo userInfo = shopDao.findUserByAc(shop.master_id);
        if (userInfo == null) {
            ReturnT returnT = ReturnT.errorReturn;
            returnT.setMsg("该店主不存在");
            return returnT;
        }
        if (0 < shopDao.searchByShopId(shop.getShop_id())){
            ReturnT returnT = ReturnT.errorReturn;
            returnT.setMsg("该账号已被注册");
            return returnT;
        }
        if (0 < shopDao.searchByShopNameAndAccount(userInfo.getUser_account(), shop.getShop_name())) {
            ReturnT retrunT= ReturnT.errorReturn;
            retrunT.setMsg("该用户已有该店");
            return retrunT;
        }
        boolean is_vip = userInfo.getIs_vip();
        // 如果不是vip且大于了普通用户创建的最大数目
        if (!is_vip && shopDao.searchByAccount(shop.getMaster_id()) >= Constants.MAX_PUBLIC_SHOP) {
            ReturnT returnT=ReturnT.errorReturn;
            returnT.setMsg("您是普通用户，只能拥有最多" + Constants.MAX_PUBLIC_SHOP + "家店铺，如想继续开店，请开通vip无上限");
            return returnT;
        }
        if( 0< shopDao.save(shop)){
            userInfo.setIs_seller(true);
            shopDao.updateById(userInfo);
            return new ReturnT<>(Code.SUCCESS, "申请成功，等待验证");
        }else {
            return ReturnT.errorReturn;
        }
    }

    @Override
    public ReturnT<?> update(Shop shop){
        //检查修改店名时，店主是否已经拥有该店名
        //检查修改店名时，新店名是否与旧店名一致
        if (!StringUtils.isBlank(shop.getShop_name())) {
            if (0 < shopDao.searchByShopNameAndAccount(shop.getMaster_id(), shop.getShop_name())) {
                ReturnT returnT = ReturnT.errorReturn;
                returnT.setMsg("该用户已有该店名，请重新修改");
                return returnT;
            }
        }
        if (0 < shopDao.updateByShopId(shop)){
            return new ReturnT<>(Code.SUCCESS,"修改成功");
        }else {
            return ReturnT.errorReturn;
        }
    }

}
